package com.sgb.goods.view.activity;

import android.view.inputmethod.EditorInfo;

import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.KeyboardUtils;
import com.google.android.material.tabs.TabLayoutMediator;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityShopMyPriceBinding;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.utils.base.adapter.BasePagerAdapter;
import com.sgb.goods.view.fragment.ShopMyPriceFragment;
import com.sgb.goods.viewmodel.ShopMyPriceViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的报价、报价询价 列表
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_SHOP_MY_PRICE)
public class ShopMyPriceActivity extends BaseActivity<GoodsActivityShopMyPriceBinding, ShopMyPriceViewModel> {

    /** tabName */
    private final String[] orderArray = {"邀请报价", "已报价", "已失效"};
    private final int[] array = {1, 2, 4};

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_shop_my_price;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(ShopMyPriceViewModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        List<Fragment> list = getFragments();
        BasePagerAdapter<Fragment> adapter = new BasePagerAdapter<>(this);
        mDataBinding.vpView.setAdapter(adapter);
        mDataBinding.vpView.setOffscreenPageLimit(list.size());
        new TabLayoutMediator(mDataBinding.tabLayout, mDataBinding.vpView, (tab, position) ->
                tab.setText(orderArray[position])).attach();
        adapter.setDataList(list);
    }

    /** 创建fragment */
    protected List<Fragment> getFragments() {
        List<Fragment> fragments = new ArrayList<>();
        for(int type : array) {
            fragments.add(new ShopMyPriceFragment(type));
        }
        return fragments;
    }

    @Override
    protected void initListener() {
        mDataBinding.backView.setOnClickListener(v -> finish());
        mDataBinding.searchShopPrice.setOnEditorActionListener((v, actionId, event) -> {
            if(EditorInfo.IME_ACTION_SEARCH == actionId) {
                mDataBinding.searchShopPrice.setFocusableInTouchMode(false);
                KeyboardUtils.hideSoftInput(mDataBinding.searchShopPrice);
                mViewModel.searchContent.setValue(v.getText().toString().trim());
                return true;
            }
            return false;
        });
    }
}

